﻿using Autofac;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintRecord;

namespace ZoneLine.Printers
{
    public class SQQCPrinter : IPrinter
    {
        public string PrintLabel(string path, string printerName, int startIndex, int? endIndex = null)
        {
            List<string> codeList = new List<string>();
            if (endIndex != null && endIndex >= startIndex)
            {
                for (int i = startIndex; i <= endIndex; i++)
                {
                    var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
                    var (paras, code) = GetPara(i);
                    PrinterHelper.PrinterLabel(path, printerName, paras);
                    codeList.Add(code);
                    Logger.Log.Info($"SQQCPrinter打印成功，第{i}个，编码：{code}");
                }
            }
            else {
                var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
                var (paras, code) = GetPara(startIndex);
                PrinterHelper.PrinterLabel(path, printerName, paras);
                codeList.Add(code);
            }
            return string.Join(",", codeList);
        }

        public void PrintLabelShow(string path, string printerName, int startIndex, int? endIndex = null)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var (paras, code) = GetPara(startIndex);
            PrinterHelper.PrinterLabelShow(path, printerName, paras);
        }

        private (Dictionary<string, string>, string) GetPara(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var configD = config.Sqqc_Config;
            var paras = new Dictionary<string, string>();
            paras.Add("FXH_N2", configD.ValveNumber);
            // 可追溯条码 生成
            var serial = GetSerial(index);
            paras.Add("KZSHM_N2", serial);
            paras.Add("SCRQ_N2", DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00"));
            paras.Add("GYSJC_N2", configD.SupplierAbbreviation);
            //var para = StationEnableConfig.GetPara();
            string code = "";
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                paras.Add("JQFX_N2", "L");
                paras.Add("LBJHM_N2", configD.PartNoL);
                var partNumTemp = configD.PartNoL.Substring(configD.PartNoL.Length - 4);
                code = $"{configD.PartIdentificationCode}{partNumTemp}{configD.SupplierCode}{serial}";
                paras.Add("EWM_N2", code);
            }
            else
            {
                paras.Add("JQFX_N2", "R");
                paras.Add("LBJHM_N2", configD.PartNoR);
                var partNumTemp = configD.PartNoR.Substring(configD.PartNoR.Length - 4);
                code = $"{configD.PartIdentificationCode}{partNumTemp}{configD.SupplierCode}{serial}";
                paras.Add("EWM_N2", code);
            }
            SQRecord sQRecord = new SQRecord();
            sQRecord.Code = code;
            sQRecord.SerialNumber = index;
            sQRecord.PartIdentificationCode = configD.PartIdentificationCode;
            sQRecord.PartNoL = configD.PartNoL;
            sQRecord.PartNoR = configD.PartNoR;
            sQRecord.SupplierAbbreviation = configD.SupplierAbbreviation;
            sQRecord.SupplierCode = configD.SupplierCode;
            sQRecord.TemplatePath = configD.TemplatePath;
            sQRecord.TemplatePathR = configD.TemplatePathR;
            sQRecord.TraceabilityNumber = configD.TraceabilityNumber;
            sQRecord.ValveNumber = configD.ValveNumber;
            var id = DBContext.DbConfig.DbContext.Insert<SQRecord>(sQRecord).ExecuteIdentity();
            return (paras, code);
        }

        private string GetSerial(int index)
        {
            var s = (byte)((DateTime.Now.Year - 2010) + 64);
            //var year = Encoding.ASCII.GetString(new byte[] { s });
            var year = YearStr.FirstOrDefault(o => o.year == DateTime.Now.Year).yearStr;
            var mouth = DateTime.Now.Month.ToString("X");
            var day = DateTime.Now.Date.ToString("dd");

            //var station=ContainerServer.Container.Resolve<>

            return $"{year}{mouth}{day}{index.ToString("0000")}";
        }


        private List<(int year, string yearStr)> YearStr = new List<(int, string)>() {
            (2025,"S"),
            (2026,"T"),
            (2027,"V"),
            (2028,"W"),
            (2029,"X"),
            (2030,"Y"),
       };
    }
}